--We need to include the other files
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include('shared.lua')

--This is what happens when the entity is created
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "sent_ball" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

--The initialization of the ent
function ENT:Initialize()
	--Set the model of the ent
	self:SetModel("models/Dodgeball.mdl")
	--Set the physics type
	self:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	self.NextThink = CurTime() + 1
	self.Entity:SetOwner(NUL)
	self.Entity:DrawShadow(true)
	--Enable the physics
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(40)
	end
	--Load some sounds for the ent to make
	self.Hit = 	Sound("Rubber.BulletImpact");

	self.FleshHit = Sound("Rubber.BulletImpact");
	
	self:GetPhysicsObject():SetMass(40)	

	//self.Entity:SetUseType(SIMPLE_USE)
end

function ENT:Think()
	
	//self.lifetime = self.lifetime or CurTime() + 20
	//I will delete myself if my life is up.
	//if CurTime() > self.lifetime then
	//	self:Remove()
	//end
end

/*---------------------------------------------------------
   Name: ENT:PhysicsCollided() // I have hit somthing, what is it?
---------------------------------------------------------*/
function ENT:PhysicsCollide(data, phys)
	--We want to do nothing here, hence the name Ball Disabled
end

--When a player presses the E button
function ENT:Use(activator, caller)
	--Destroy myself
	if (activator:Team() != TEAM_SPECTATOR) then
		self.Entity:Remove()
		--Get who is using me
		if (activator:IsPlayer()) then
			--if they do not have the throwing arm weapon then give them one
			if activator:GetWeapon("weapon_throwingarm") == NULL then
				activator:Give("weapon_throwingarm")
			else
				--Otherwise just give them some ammo
				activator:GiveAmmo(1, "Grenade")
			end
		end
	end
end

--What happens when the ent is disabled
function ENT:Disable()
	self.PhysicsCollide = function() end
	self.lifetime = CurTime() + 30
	self:GetPhysicsObject():SetMass(1)
end